<template>
  <div class="item" :class="{ mobile: type, pc: !type }">
    <img v-lazy="formatUrl(source.url_thumb, source.resolution)" alt="" />
  </div>
</template>

<script>
export default {
  name: "item",
  props: {
    source: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      type: true // 默认是移动端
    }
  },
  created() {
    this.type = this.$Utils.isMoible()
  },
  methods: {
    formatUrl(url, resolution) {
      const type = this.$Utils.isMoible()
      const visibleWidth = document.body.clientWidth
      const tempArr = resolution.split("x")
      let ratio = 1,
        imgWidth = "",
        imgHeight = "",
        targetUrl = ""
      if (type) {
        imgWidth = visibleWidth
      } else {
        imgWidth = 1920
      }
      ratio = tempArr[0] / imgWidth
      imgHeight = parseInt(tempArr[1] / ratio)
      targetUrl = `/bdm/${imgWidth}_${imgHeight}_85/`
      return url.replace("/bdr/__85/", targetUrl)
    }
  }
}
</script>

<style scoped lang="less">
.item {
  width: 100%;
  position: relative;
  background-color: #ccc;
  border-top: 0.5px solid #fff;
  border-bottom: 0.5px solid #fff;
  &.pc {
    height: calc(100vh - 98px);
    img {
      height: 100%;
    }
  }
  &.mobile {
    height: 200px;
    img {
      height: 200px;
    }
  }
  img {
    width: 100%;
    display: block;
    object-fit: cover;
  }
}

img[lazy="error"] {
  width: 25px !important;
  height: 25px !important;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

img[lazy="loading"] {
  width: 25px !important;
  height: 25px !important;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
</style>
